package com.cskaoyan.service.wx.home;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.wx.home.HomeIndexVo;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.bean.vo.wx.home.indexFloorGoodsVo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/08/23:56
 * @Description:
 */
@Service
public class HomeServiceImpl implements HomeService {
    private static final String NEW_GOODS1="market_wx_index_new";
    private static final String HOT_GOODS1="market_wx_index_hot";
    private static final Integer NEW_GOODS=10;
    private static final Integer HOT_GOODS=6;
    private static final Integer BRAND_NUM=4;
    private static final String BRAND_NUM1="market_wx_index_brand";
    private static final Integer CATE_GOODS=5;
    private static final String CATE_GOODS1="market_wx_catlog_goods";
    private static final int CATE_LIMIT = 5;
    private static final String CATE_LIMIT1 = "market_wx_catlog_list";
    private static final String TOPIC = "market_wx_index_topic";
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketSystemMapper systemMapper;

    /**
    * @Author: 宾文杰 @Description: 全部商品信息 @Date: 2022/9/9
    */
    @Override
    public HomeIndexVo homeIndex() {
        String newGoods = systemMapper.selectFreightPriceInfo(NEW_GOODS1);
        String hotGoods = systemMapper.selectFreightPriceInfo(HOT_GOODS1);
        String brandNum = systemMapper.selectFreightPriceInfo(BRAND_NUM1);
        String cateGoods = systemMapper.selectFreightPriceInfo(CATE_GOODS1);
        String cateNum = systemMapper.selectFreightPriceInfo(CATE_LIMIT1);
        String topicNum = systemMapper.selectFreightPriceInfo(TOPIC);

        HomeIndexVo homeIndexVo = new HomeIndexVo();

        //广告商信息
        MarketAdExample marketAdExample = new MarketAdExample();
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        homeIndexVo.setBanner(marketAds.toArray());

        //制造商信息
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        PageHelper.startPage(1, Integer.parseInt(brandNum));
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        homeIndexVo.setBrandList(marketBrands.toArray());

        //类别信息
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        homeIndexVo.setChannel(marketCategories.toArray());

        //展示团购信息
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        homeIndexVo.setCouponList(marketCoupons.toArray());

        //展示前几个类别的商品
        ArrayList<indexFloorGoodsVo> indexFloorGoodsVos = new ArrayList<>();
        for (int i = 0; i < marketCategories.size()&&i<Integer.parseInt(cateNum); i++) {
            indexFloorGoodsVo indexFloorGoodsVo = new indexFloorGoodsVo();
            indexFloorGoodsVo.setId(marketCategories.get(i).getId());
            indexFloorGoodsVo.setName(marketCategories.get(i).getName());
            //找二级目录
            Integer pid = marketCategories.get(i).getId();
            MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
            marketCategoryExample1.createCriteria().andPidEqualTo(pid);
            List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
            ArrayList<MarketGoods> allMarketGoods = new ArrayList<>();
            for (MarketCategory category : marketCategories1) {
                Integer categoryId = category.getId();
                MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
                MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
                criteria1.andCategoryIdEqualTo(categoryId);
                List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
                for (MarketGoods marketGood : marketGoods) {
                    allMarketGoods.add(marketGood);
                }
            }
            //二级目录的几个商品信息
            ArrayList<MarketGoods> marketGoodsLimit = new ArrayList<>();
            for (int i1 = 0; i1 < allMarketGoods.size()&&i1<Integer.parseInt(cateGoods); i1++) {
                marketGoodsLimit.add(allMarketGoods.get(i1));
            }
            indexFloorGoodsVo.setGoodsList(marketGoodsLimit.toArray());
            indexFloorGoodsVos.add(indexFloorGoodsVo);
        }
        homeIndexVo.setFloorGoodsList(indexFloorGoodsVos.toArray());

        //人气商品
        PageHelper.startPage(1,Integer.parseInt(hotGoods));
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
        criteria1.andIsHotEqualTo(true);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        homeIndexVo.setHotGoodsList(marketGoods.toArray());

        //新品
        PageHelper.startPage(1,Integer.parseInt(newGoods));
        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        marketGoodsExample1.createCriteria().andIsNewEqualTo(true);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample1);
        homeIndexVo.setNewGoodsList(marketGoods1.toArray());

        //提示
        PageHelper.startPage(1,Integer.parseInt(topicNum));
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        homeIndexVo.setTopicList(marketTopics.toArray());
        return homeIndexVo;
    }
}
